@extends('layouts.header')
@section('title','带电作业系统-关系映射')
@section('content')
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 基础信息 <span
        class="c-gray en">&gt;</span> 带电作业系统
    <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<article class="page-container">
    <div id="tab_demo" class="HuiTab">
        <div class="tabBar clearfix"><span>工器具类型及项目数据</span><span class="ml-5">检测标准</span><span class="ml-5">检测项目</span><span class="ml-5">额定电压和分类</span></div>
        <!-- 工器具类型 -->
        <div class="tabCon">
            <div class="cl pd-5 bg-1 bk-gray">
                <span class="">
                    <a href="javascript:;" onclick="autoAdd('tool')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 自动添加工器具类型</a>
                </span>
                <form id="toolForm" class="form form-horizontal">
                    <div class="row cl">
                        <label class="form-label col-xs-3">带电作业-工器具类型：</label>
                        <div class="formControls col-xs-8">
                            <input type="text" name="name" class="input-text required " maxlength="20" />
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-3">本系统-工器具：</label>
                        <div class="formControls col-xs-8">
                            <select name="tool_name" class="select2 input-text required" maxlength="20" style="width:70%;">
                                @foreach($data['tools'] as $name)
                                <option value="{{$name}}">{{$name}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="row cl text-c">
                        <a class="btn btn-primary radius" id="mapTool">添加</a>
                    </div>
                </form>
            </div>
            <table class="table table-border table-bordered table-hover table-bg table-responsive" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>ID</th>
                        <th>带电作业-工器具名称</th>
                        <th>本系统-工器具名称</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['sdTools'] as $item)
                    <tr class="text-c">
                        <td>{{$item->id}}</td>
                        <td>{{$item->name}}</td>
                        <td>
                            <form>
                                <select name="tool_name" class="select2" style="width:80%;">
                                    @foreach($data['tools'] as $val)
                                        <option value="{{$val}}" @if($item->tool_name == $val) selected="selected" @endif>{{$val}}</option>
                                    @endforeach
                                </select>
                                <input type="hidden" name="id" value="{{$item->id}}">
                                <span class="btn btn-primary radius size-MINI" onclick="mapTool(this)">修改</span>
                            </form>
                        </td>
                        <td>
                            <span class="btn btn-secondary radius size-MINI" onclick="layer_show('{{$item->name}} 映射项目数据','mapItemKey?id={{$item->id}}')">映射项目数据</span>
                            <span class="btn btn-primary radius size-MINI" onclick="del(this,'tool',{{$item->id}})">删除</span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <!-- 检测标准 -->
        <div class="tabCon">
            <div class="cl pd-5 bg-1 bk-gray">
                <form class="form form-horizontal" id="stdForm">
                    <div class="row cl">
                        <label class="form-label col-sm-3">带电作业-试验标准代号：</label>
                        <div class="formControls col-xs-8">
                            <input type="text" name="num" class="input-text required">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-3">本系统-检测标准：</label>
                        <div class="formControls col-xs-8">
                            <select name="std_num" class="select2 required" style="width:80%;">
                                @foreach($data['stdNums'] as $val)
                                    <option value="{{$val}}">{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="row cl text-c">
                        <span class="btn btn-primary radius" id="mapStd">添加</span>
                    </div>
                </form>
            </div>
            <table class="table table-border table-bordered table-hover table-bg table-responsive mt-10" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>带电作业检测标准</th>
                        <th>本系统检测标准</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['sdStdNums'] as $item)
                    <tr class="text-c">
                        <td>{{$item->num}}</td>
                        <td>
                            <form>
                                <select name="std_num" class="select2" style="width:80%;">
                                    @foreach($data['stdNums'] as $val)
                                        <option value="{{$val}}" @if($item->std_num == $val) selected="selected" @endif>{{$val}}</option>
                                    @endforeach
                                </select>
                                <input type="hidden" name="id" value="{{$item->id}}">
                                <span class="btn btn-primary radius size-MINI" onclick="mapStd(this)">修改</span>
                            </form>
                        </td>
                        <td>
                            <span class="btn btn-primary radius size-MINI" onclick="del(this,'std',{{$item->id}})">删除</span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <!-- 检测项目 -->
        <div class="tabCon">
            <div class="cl pd-5 bg-1 bk-gray">
                <span class="">
                    <a href="javascript:;" onclick="autoAdd('testitem')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 自动添加检测项目</a>
                    <a href="javascript:;" onclick="layer_show('添加检测项目','mapTestitem')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加检测项目</a>
                </span>
            </div>
            <table class="table table-border table-bordered table-hover table-bg table-responsive" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>带电作业项目ID</th>
                        <th>带电作业检测项目</th>
                        <th>映射检测项目</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['sdTestitem'] as $item)
                    <tr class="text-c">
                        <td>{{$item->ddzy_id}}</td>
                        <td>{{$item->name}}</td>
                        <td>
                            <form>
                                <select name="testitem_name[]" multiple="multiple"  class="select2" style="width:80%;">
                                    @foreach($data['testitem'] as $val)
                                        <option value="{{$val}}" @if(strpos(",{$item->testitem_name},",",{$val},") !== false) selected="selected" @endif>{{$val}}</option>
                                    @endforeach
                                </select>
                                <input type="hidden" name="id" value="{{$item->id}}">
                                <span class="btn btn-primary radius size-MINI" onclick="mapTestitem(this)">修改</span>
                            </form>
                        </td>
                        <td>
                            <span class="btn btn-primary radius size-xs" onclick="del(this,'testitem',{{$item->id}})">删除</span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <!-- 额定电压和分类 -->
        <div class="tabCon">
            <div class="cl pd-5 bg-1 bk-gray">
                <form class="form form-horizontal" id="volClassForm">
                    <div class="row cl">
                        <label class="form-label col-sm-3">类型：</label>
                        <div class="formControls col-xs-8">
                            <label><input type="radio" name="type" checked value="0">额定电压</label>
                            <label class="ml-10"><input type="radio" name="type" value="1">分类</label>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-3">带电作业-值：</label>
                        <div class="formControls col-xs-8">
                            <input type="text" name="val" class="input-text required">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-3">本系统-值：</label>
                        <div class="formControls col-xs-8">
                            额定电压：
                            <select name="voltage" class="select2 " style="width:150px;">
                                <option value="">--请选择--</option>
                                @foreach($data['volClass'][0] as $val)
                                    <option value="{{$val}}">{{$val}}</option>
                                @endforeach
                            </select>
                            分类：
                            <select name="class" class="select2 " style="width:250px;">
                                <option value="">--请选择--</option>
                                @foreach($data['volClass'][1] as $val)
                                    <option value="{{$val}}">{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="row cl text-c">
                        <span class="btn btn-primary radius" id="mapVolClass">添加</span>
                    </div>
                </form>
            </div>
            <table class="table table-border table-bordered table-hover table-bg table-responsive mt-10" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>类型</th>
                        <th>带电作业-值</th>
                        <th>本系统-值</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['sdVolClass'] as $item)
                    <tr class="text-c">
                        <td>{{$item->type === 0 ? '额定电压' : '分类'}}</td>
                        <td>{{$item->val}}</td>
                        <td>
                            <form>
                                <select name="vol_class" class="select2 required" style="width:80%;">
                                    @foreach($data['volClass'][$item->type] as $val)
                                        <option value="{{$val}}" @if($item->vol_class == $val) selected="selected" @endif>{{$val}}</option>
                                    @endforeach
                                </select>
                                <input type="hidden" name="id" value="{{$item->id}}">
                                <span class="btn btn-primary radius size-MINI" onclick="mapVolClass(this)">修改</span>
                            </form>
                        </td>
                        <td>
                            <span class="btn btn-primary radius size-MINI" onclick="del(this,'volClass',{{$item->id}})">删除</span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</article>

<script type="text/javascript">
    $(function () {
        $('.select2').select2();
        $('#refresh').trigger('click');
        $("#tab_demo").Huitab({
            index: localStorage.getItem('sdDdzyIndexI',0),
        });
        $('.tabBar',"#tab_demo").on('click',function () {
            let index = 0,spans = $(this).children('span');
            spans.each(function () {
                if($(this).hasClass('current')){
                    localStorage.setItem('sdDdzyIndexI',index);
                }
                index += 1;
            })
        })
    })
    
    //删除
    function del(obj,type,id){
        ajax('post','del',{type:type,id:id},delBak);
        function delBak(res){
            if(parseInt(res) === 1){
                $(obj).closest('tr').remove();
            }
        }
    }
    //映射工器具类型-添加
    $('#mapTool').on('click',function(){
        if ($('#toolForm').valid()) {
            ajax('post','mapTool',$('#toolForm').serialize(),mapToolBak);
            function mapToolBak(res){
                if (parseInt(res) == 1) {
                    location.reload();
                }
            }
        }
    })
    //映射工器具类型-修改
    function mapTool(obj){
        let formData = $(obj).closest('form').serialize();
        ajax('post','mapTool',formData);
    }
    //映射检测项目编辑
    function mapTestitem(obj){
        let formData = $(obj).closest('form').serialize();
        ajax('post','mapTestitem',formData);
    }
    //自动添加
    function autoAdd(type='tool'){
        ajax('post','autoAdd',{type:type},autoAddBak);
        function autoAddBak(res){
            if(parseInt(res) === 1){
                location.reload();
            }
        }
    }
    //映射标准-添加
    $('#mapStd').on('click',function (){
        let form = $('#stdForm');
            formData = form.serialize();
        if (form.valid()) {
            ajax('post','mapStd',formData,mapStdBak);
            function mapStdBak(res){
                if (parseInt(res) == 1) {
                    location.reload();
                }
            }
        }
    })
    //映射标准-修改
    function mapStd(obj) {
        let formData = $(obj).closest('form').serialize();
        ajax('post','mapStd',formData);
    }
    //映射额定电压和分类-添加
    $('#mapVolClass').on('click',function (){
        let form = $('#volClassForm');
            formData = form.serialize();
        if (form.valid()) {
            ajax('post','mapVolClass',formData,mapVolClassBak);
            function mapVolClassBak(res){
                if (parseInt(res) == 1) {
                    location.reload();
                }
            }
        }
    })
    //映射额定电压和分类-修改
    function mapVolClass(obj) {
        let form = $(obj).closest('form'),
            formData = form.serialize();
        if (form.valid()) {
            ajax('post','mapVolClass',formData);
        }else{
            layer.msg('请选系统值');
        }
    }
</script>
@endsection